package com.open.party.cloud.view.home.jiCengDangJian.partyService

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppTitleBaseActivity
import com.gyf.immersionbar.ImmersionBar
import com.open.party.cloud.R
import com.open.party.cloud.model.ZyzJiFenBean
import com.open.party.cloud.viewModel.ZiYuanZheViewModel
import com.sinothk.android.utils.XUtils
import com.open.party.cloud.view.home.jiCengDangJian.fragment.ZyzJiFenAllListFragment
import com.open.party.cloud.view.home.jiCengDangJian.fragment.ZyzJiFenMonthListFragment
import kotlinx.android.synthetic.main.activity_zyz_jifen_main.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 */
class ZyzJiFenMainActivity : AppTitleBaseActivity(), View.OnClickListener {

    private var ziYuanZheVM: ZiYuanZheViewModel? = null
    override fun getLayoutResId(): Int = R.layout.activity_zyz_jifen_main

    private var adapter: ArrayAdapter<String>? = null
    private var streetStrList: ArrayList<String>? = null

    var isFirst = true
    var currFragment = 0

    private var fragment0: ZyzJiFenAllListFragment? = null
    private var fragment1: ZyzJiFenMonthListFragment? = null

    override fun onResume() {
        super.onResume()
        EventBus.getDefault().register(this)
    }

    override fun onPause() {
        super.onPause()
        EventBus.getDefault().unregister(this)
    }

    override fun setViewTitle(centerTxt: String?) {
        super.setViewTitle(centerTxt)
        titleBarView.setLeftIcon(R.drawable.icon_back_white)
        titleBarView.setCenterTxtColor(R.color.white)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImmersionBar.with(this)
            .statusBarDarkFont(false).init()
        setViewTitle("积分榜")
        ziYuanZheVM = ZiYuanZheViewModel()
        initView()
        initListener()
    }

    private fun initView() {
        fragment0 = ZyzJiFenAllListFragment()
        fragment1 = ZyzJiFenMonthListFragment()
        val fragments = ArrayList<Fragment>()
        fragments.add(fragment0!!)
        fragments.add(fragment1!!)

        val fragmentPagerAdapter = object : FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(arg0: Int): Fragment {
                return fragments[arg0]
            }
        }

        viewPager.offscreenPageLimit = fragments.size - 1
        viewPager.adapter = fragmentPagerAdapter
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                when (position) {
                    0 -> {
                        currFragment = 0
                        tab01Txt.setTextColor(ContextCompat.getColor(this@ZyzJiFenMainActivity, R.color.app_jifen_selected))
                        tab01Line.setBackgroundResource(R.color.app_jifen_selected)
                        tab02Txt.setTextColor(ContextCompat.getColor(this@ZyzJiFenMainActivity, R.color.app_jifen_unselected))
                        tab02Line.setBackgroundResource(R.color.transparent)
                    }
                    1 -> {
                        currFragment = 1
                        tab01Txt.setTextColor(ContextCompat.getColor(this@ZyzJiFenMainActivity, R.color.app_jifen_unselected))
                        tab01Line.setBackgroundResource(R.color.transparent)
                        tab02Txt.setTextColor(ContextCompat.getColor(this@ZyzJiFenMainActivity, R.color.app_jifen_selected))
                        tab02Line.setBackgroundResource(R.color.app_jifen_selected)
                    }
                }
            }
        })

        Thread {
            ziYuanZheVM!!.getZyzJiFenStreetListData()
        }.start()
    }

    private fun initListener() {
        tab01View.setOnClickListener(this)
        tab02View.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v) {
            tab01View -> {
                viewPager.setCurrentItem(0, true)
            }
            tab02View -> {
                viewPager.setCurrentItem(1, true)
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<ArrayList<ZyzJiFenBean>>) {

        if (result.eventType != "getZyzJiFenStreetListData") {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                initStreetSpinner(result.data)
            }
            ResultInfo.TOKEN_OVERDUE -> {
                // 不存在
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    private fun initStreetSpinner(streetList: ArrayList<ZyzJiFenBean>?) {
        if (streetList == null || streetList.size == 0) {
            return
        } else {
            streetStrList = ArrayList()
        }

        for (index in streetList.indices) {
            streetStrList!!.add(streetList[index].orgName)
        }

        streetStrList!!.add(0, "全部")

        adapter = ArrayAdapter(this, R.layout.street_spinner_item, streetStrList!!)
        adapter!!.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        streetSpinner.adapter = adapter
        streetSpinner.dropDownVerticalOffset = 100
        streetSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                val tv = view as TextView
                tv.setTextColor(ContextCompat.getColor(this@ZyzJiFenMainActivity, R.color.white))
                tv.textSize = 12f
                if (isFirst) {
                    isFirst = false
                    return
                }

                if ("全部" == streetStrList!![position]) {
                    refreshFragmentData("")
                } else {
                    if ((position - 1) < streetList.size) {// 去除手动添加的“全部”选项
                        refreshFragmentData(streetList[position - 1].id)
                    }
                }
            }
        }
    }

    private fun refreshFragmentData(orgId: String) {
//        when (currFragment) {
//            0 -> {
//                fragment0?.updateData(orgId)
//            }
//            1 -> {
//                fragment1?.updateData(orgId)
//            }
//        }
        fragment0?.updateData(orgId)
        fragment1?.updateData(orgId)
    }
}